<?php 


     /* 
     ############################################################################################# 
     ####                                                                                     #### 
     ####    Author : Harish Chauhan, Modified by Alec Gorge for use with SQLite 3            #### 
     ####    Date   : 29 Sep, 2004                                                            #### 
     ####    Updated: 17 March, 2009                                                          ####
     ####                                                                                     #### 
     #############################################################################################
     */ 

    class DB 
    { 
        // Connection parameters  
             
        var $database = '';///database i.e ../database/first_step.db 
        var $persistent = false; 
       
         
        // Database connection handle  
        var $conn = NULL; 

        // Query result  
        var $result = false; 
		private $cache = false;

        function DB() 
        { 
        } 
        function open() 
        { 
            // Connect to the SQLite3 Database
			// Have to use PDO for PHP 5.2.* compatibility
			try {
				$this->conn = new PDO('sqlite:'.$this->database); 
				return true; 
			}
			catch (PDOException $e) {
                echo $e->getMessage();
				return false;
			}
        } 

        function close() 
        { 
            return ($this->conn = null); 
        } 

        function error() 
        { 
            return end($this->conn->errorInfo()); 
        } 

        function query($sql = '') { 
			$mode = strtoupper(reset(explode(' ', $sql)));
			// Timing
 			$queryStart = round(microtime(), 4);
			
			// Query
  			if($mode == 'SELECT') {
				$x = $this->conn->query($sql);
				$this->result = $x; 	
			}
			else {
				$x = $this->conn->exec($sql);
				$this->result = $x; 	
			}
			
			// Timing End
			$queryEnd = round(microtime(), 4);
			$queryTime = $queryEnd - $queryStart;		
			$GLOBALS['MYSQL_NUM_QUERIES']++;
			$GLOBALS['MYSQL_QUERY_TIME'] += $queryTime;
			
			// Debug Log
			$call_info = array_shift( debug_backtrace() );
			$code_line = $call_info['line'];
			$file = array_pop( explode('/', $call_info['file']));
			$GLOBALS['mysql_debug'] .= '<strong>Ran Query #'.$GLOBALS['MYSQL_NUM_QUERIES']." (Line $code_line of ".basename($file).") (Time $queryTime): </strong>$sql<br />";
			
            return $this->result; 
       } 

        function affectedRows($x ='') 
        { 
   			if(!empty($x))
				return $x;
           return $this->result; 
        } 
		function cacheResult () {
			if($this->cache == false)
				$this->cache = $this->result->fetchAll();
			else
				return $this->cache;
		}
        function numRows($x ='') 
        {
  			if(!empty($x))
				return count($x);
			$x = count($this->result); 
			return $x;
        } 
        /*function fieldName($field) 
        { 
           return (@sqlite_field_name($this->result,$field)); 
        } */
        function insertID($x ='') 
        { 
            return $this->conn->lastInsertId(); 
        } 
         
        function fetchObject($x ='') 
        { 
			if(!empty($x))
				return $x->fetch(PDO::FETCH_OBJ);
            return $this->result->fetch(PDO::FETCH_OBJ);
        } 

        function fetchArray($x ='') 
        {
			if(!empty($x))
				return $x->fetch(PDO::FETCH_BOTH);
            return $this->result->fetch(PDO::FETCH_BOTH);
       } 

        function fetchAssoc($x ='') 
        { 
 			if(!empty($x))
				return $x->fetch(PDO::FETCH_ASSOC);
           return $this->result->fetch(PDO::FETCH_ASSOC);
        } 

    } 
?>